package cn.chenxiejia;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/***
 * @program: maven
 * @ClassName: Imges4
 * @Description: 验证码的实现
 * @Author: chenxiejia
 * @Date: 2022/1/22 0:16
 * @version : V1.0
 */
public class Imges4 {
    public static void main(String[] args) throws IOException {
        Random rand = new Random();
        //画板
        BufferedImage i = new BufferedImage(200, 100, 1);
        //画笔
        var m = i.createGraphics();
        //平滑度
        m.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        m.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        m.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        m.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(150, 200)));
        m.fillRect(0, 0, 200, 100);
        Font font = new Font("宋体", Font.BOLD, 50);
        m.setFont(font);
        var fm = m.getFontMetrics(font);
        //验证字符
        String setter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();
        //生成随机验证码
        for (int j = 0; j < 6; j++) {
            //随机颜色
            m.setColor(new Color(rand.nextInt(120, 255), rand.nextInt(120, 255), rand.nextInt(120, 255), 255));
            int x = j * 30 + 10;
            //验证字符位置
            int y1 = i.getHeight() / 2 + (fm.getAscent() - fm.getDescent()) / 2 - fm.getHeight() / 3;
            int y2 = i.getHeight() / 2 + (fm.getAscent() - fm.getDescent()) / 2 + fm.getHeight() / 3;
            int a = rand.nextInt(setter.length());
            font.deriveFont(rand.nextInt(45, 55));
            var s = setter.substring(a, a + 1);
            m.drawString(s, x, rand.nextInt(y1, y2));
            sb.append(s);
        }

        //验证码干扰
        for (int j = 0; j < 10; j++) {
            m.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(50, 120)));
            int a = rand.nextInt(setter.length());
            var s = setter.substring(a, a + 1);
            m.drawString(s, rand.nextInt(10, 190), rand.nextInt(10, 90));
        }

        for (int j = 0; j < 3; j++) {
            m.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(100, 200)));
            m.setStroke(new BasicStroke(3));
            m.drawLine(rand.nextInt(10, 20), rand.nextInt(20, 90), rand.nextInt(170, 190), rand.nextInt(20, 90));

        }
        //曲线干扰
        Point[] points = {new Point(0, 0), new Point(0, rand.nextInt(100)), new Point(200, rand.nextInt(100))

        };

        GeneralPath path = new GeneralPath();
        path.moveTo(points[0].x, points[0].y);
        for (int i2 = 0; i2 < points.length - 1; ++i2) {
            Point sp = points[i2];
            Point ep = points[i2 + 1];
            Point c1 = new Point((sp.x + ep.x) / 2, sp.y);
            Point c2 = new Point((sp.x + ep.x) / 2, ep.y);
            path.curveTo(c1.x, c1.y, c2.x, c2.y, ep.x, ep.y);
        }

        // g.draw(path);
        for (int n = 0; n <= 355; n += 10) {
            // g.drawArc(100, 200, 600, 300, n, 5);
        }

        int yy = 100;// y
        int hh = 100;// 高度
        int aa = 200;//
        // x
        for (int x = 10; x < 200; x += 15) {
            int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
            //g.drawLine(x, (int) y, x, (int) y);
            m.fillOval(x, y, 6, 6);
        }

        m.dispose();
        ImageIO.write(i, "jpg", new File("yanzheng.jpg"));
        Runtime.getRuntime().exec("cmd /k start yanzheng.jpg");

    }
}
